package com.market.sys.api.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.market.sys.common.Result;
import com.market.sys.dto.UserSearchDto;
import com.market.sys.entity.User;
import com.market.sys.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author xd
 * @since 2024-03-26
 */
@RestController
@RequestMapping("/sys/admin/wxUser")
public class AdminUserController {

    @Autowired
    private IUserService userService;

    @PostMapping("/list")
    public Result<Map<String ,Object>> search(@RequestBody UserSearchDto form){
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StrUtil.isNotEmpty(form.getAccount()),User::getAccount,form.getAccount());
        if (form.getRoleId() != null){
            lambdaQueryWrapper.eq(User::getUserRole,form.getRoleId());
        }
        Page<User> page = userService.page(new Page<>(form.getPageNo(), form.getPageSize()), lambdaQueryWrapper);

        Map<String,Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getRecords());

        return Result.success(data);
    }

}
